from django.core.signals import request_finished
from django.dispatch import receiver
from auditbox.settings import DELETE_UPLOAD_DATA
import shutil,os
from apps.translate.config import pdftoword_base_path,pdftoexcel_base_path,pdftojpg_base_path

# 处理程序
@receiver(request_finished)
def deleteSrcData(sender, **kwargs):
        # 如果这里不删，则是false
    if DELETE_UPLOAD_DATA == True:
        if sender == 'pdftoword': # 单数据文件
            item = kwargs.get('md5Items')[0]
            os.remove(pdftoword_base_path+item+'.'+'%s'%'docx')
            os.remove(pdftoword_base_path+item+'.'+'%s'%'pdf')
        elif sender == 'pdftowords': # 多数据文件
            items = kwargs.get('md5Items')[0]
            new_path = os.path.join(pdftoword_base_path,items)
            shutil.rmtree(new_path,ignore_errors=False)
        if sender == 'pdftoexcel': # 单数据文件
            item = kwargs.get('md5Items')[0]
            os.remove(pdftoexcel_base_path+item+'.'+'%s'%'pdf')
            os.remove(pdftoexcel_base_path+item+'.'+'%s'%'xlsx')
        elif sender == 'pdftoexcels': # 多数据文件
            items = kwargs.get('md5Items')[0]
            new_path = os.path.join(pdftoexcel_base_path,items)
            shutil.rmtree(new_path,ignore_errors=False)
        if sender == 'pdftojpg': # 单数据文件
            item = kwargs.get('md5Items')[0]
            os.remove(pdftojpg_base_path+item+'.'+'%s'%'pdf')
            os.remove(pdftojpg_base_path+item+'.'+'%s'%'zip')
            shutil.rmtree(pdftojpg_base_path+item,ignore_errors=False)
        elif sender == 'pdftojpgs': # 多数据文件
            items = kwargs.get('md5Items')[0]
            data = kwargs.get('md5Items')[1]
            os.remove(pdftojpg_base_path+items+'.'+'%s'%'zip')
            shutil.rmtree(pdftojpg_base_path+items,ignore_errors=False)
            for k in data:
                os.remove(pdftojpg_base_path+k+'.'+'%s'%'pdf')
    else:
        pass
